package Service;

import android.app.Service;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.example.zelonghu.test_all_without_network.AppContent;

import java.util.LinkedList;
import java.util.Queue;

import Fragment.ControlFragment;
import SensorData.SensorData;

public class BackGroundService extends Service {
    private SensorEventListener gyroscopeSensorEventListener = null;
    private SensorEventListener provimitySensorEventListener = null;
    private SQLiteDatabase db = null;
    private Sensor gyroscopeSensor = null;
    private Sensor proximitySensor = null;
    private String dbtablename = null;
    private SensorManager sm = null;
    private Queue<SensorData> dataque = null;
    public BackGroundService() {

    }

    @Override
    public void onCreate() {
        Log.i("backgroundService","create service!" + this.toString());
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("backgroundService","start service!"+ this.toString());
        sm = (SensorManager)getSystemService(SENSOR_SERVICE);
        gyroscopeSensor = sm.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        proximitySensor  = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
        dataque = new LinkedList<>();
        gyroscopeSensorEventListener = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent sensorEvent) {
                Log.i("gyroscope",sensorEvent.values[0] + " "+
                        sensorEvent.values[1] + " " + sensorEvent.values[2]);
                //add new sensordata into the database
                SensorData nsd = new SensorData();
                nsd.setTime(System.currentTimeMillis() + "");
                nsd.setType(AppContent.gyroscopeSensorData);
                nsd.setValue1(sensorEvent.values[0]);
                nsd.setValue2(sensorEvent.values[1]);
                nsd.setValue3(sensorEvent.values[2]);
                Toast.makeText(BackGroundService.this, nsd.toString(), Toast.LENGTH_LONG).show();

                //先不写入Flag，等待用户点击停止标定。
                //sqllite.insertSD(db,dbtablename,nsd);
                //dataque.add(nsd);
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int i) {

            }
        };
        provimitySensorEventListener = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent sensorEvent) {
                if (sensorEvent.values[0] < proximitySensor.getMaximumRange()) {
                    //there is object
                    Log.i("proximity","something block the phone");
                    //add new sensordata into the database
                    SensorData nsd = new SensorData();
                    nsd.setTime(System.currentTimeMillis() + "");
                    nsd.setType(AppContent.ProximitySensorData);
                    nsd.setValue1(sensorEvent.values[0]);
                    Toast.makeText(BackGroundService.this, nsd.toString(), Toast.LENGTH_LONG).show();

                    //sqllite.insertSD(db,dbtablename,nsd);
                    //dataque.add(nsd);
                } else {
                    //nothing happened
                }
            }
            @Override
            public void onAccuracyChanged(Sensor sensor, int i) {
            }
        };
        sm.registerListener(gyroscopeSensorEventListener
                ,gyroscopeSensor,AppContent.RECEIVING_INTERVAL);
        sm.registerListener(provimitySensorEventListener,proximitySensor,
                AppContent.RECEIVING_INTERVAL);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        sm.unregisterListener(gyroscopeSensorEventListener);
        sm.unregisterListener(provimitySensorEventListener);
        Log.i("backgroundService","destroy service!"+ this.toString());
        Toast.makeText(BackGroundService.this, "Background Service is destroyed!", Toast.LENGTH_LONG).show();
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
